import tensorflow as tf
import os
import matplotlib.pyplot as plt


def sigmoid(x):
    return 1.0 / (1 + tf.exp(-x))


if __name__ == '__main__':
    os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
    # 准备 x 输入向量
    x = tf.linspace(-10, 10, 100)
    # print(x)

    # y = sigmoid(x)
    # print(y)

    y = tf.nn.sigmoid(x)
    grad = tf.nn.sigmoid(x) * (1 - tf.nn.sigmoid(x))

    plt.plot(x, y, color='r', label="sigmoid")
    plt.plot(x, grad, color='y', label='sigmoid gradient')
    # 设置图例
    plt.legend(loc=2)
    plt.title("sigmoid(x)")
    plt.grid()
    plt.xlabel(xlabel='x')
    plt.ylabel(ylabel='sigmoid(x) and sigmoid(x)(1-sigmoid(x))')
    plt.xlim(-10, 10)
    plt.ylim(0, 1)
    plt.show()
